import { createRouter,createWebHistory,RouteRecordRaw } from "vue-router";
import localCache from '@/utils/cache'
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect:'/login'
  },
  {
    path: '/login',
    name:'login',
    component:() => import('@/pages/login/Login.vue')
  },
  {
    path: '/main',
    name: 'main',
    component:() => import('@/pages/main/Main.vue')
  }

]
const router = createRouter({
  routes,
  history:createWebHistory()
})

router.beforeEach(to => {
  const token = localCache.getCache('token')
  
  if (token) {
    if (to.path === '/login') {
      return '/main'
    }
  } else {
    if (to.path.indexOf('/main') !== -1) {
      return '/login'
    }
  }
})

export default router